package com.bugpool.leilema.tc.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.bugpool.leilema.freamwork.satoken.SaTokenUtil;
import com.bugpool.leilema.tc.bean.entity.BoundaryEntity;
import com.bugpool.leilema.tc.bean.entity.SysMenu;
import com.bugpool.leilema.tc.bean.entity.SysUser;
import com.bugpool.leilema.tc.bean.vo.SysUserVo;
import com.bugpool.leilema.tc.mapper.MenuMapper;
import com.bugpool.leilema.tc.service.MenuService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

/**
 * @Author wh
 * @Description
 * @Date 2023/3/1 15:43
 * @Version 1.0
 **/
@Service
public class MenuServiceImpl implements MenuService {

    @Resource
    private MenuMapper menuMapper;

    @Override
    public List<Tree<Long>> getMenuListByUser() {
        SysUserVo user = SaTokenUtil.getUser();
        List<SysMenu> sysMenus = null;
        if (user.isAdmin()) {
            //超级管理员返回所有的路由信息
            sysMenus = menuMapper.selectMenuTreeAll();
            return buildMenuTree(sysMenus);
        }
        //根据角色返回部分菜单
        sysMenus = menuMapper.selectMenuByRoleId(user.getRoleIds());
        return buildMenuTree(sysMenus);
    }

    @Override
    public Set<String> getPermissions() {
        SysUserVo user = SaTokenUtil.getUser();
        //判断用户是否为超级管理员
        if (user.isAdmin()) {
            //返回所有的权限
            return menuMapper.selectPermissionsAll();
        }
        //返回对应的权限
        return menuMapper.selectPermissionsByRole(user.getRoleIds());

    }

    @Override
    public List<Tree<Long>> selectMenuTree() {
        SysUserVo user = SaTokenUtil.getUser();
        List<SysMenu> sysMenus = null;
        if (user.isAdmin()) {
            //查询所有的信息
            sysMenus = menuMapper.selectList(null);
        } else {
            //不是管理员 根据角色 查询出部分信息
            sysMenus = menuMapper.selectMenuTreeList(user.getRoleIds());
        }
        return buildTree(sysMenus);


    }


    @Override
    public Boolean updateMenu(SysMenu sysMenu) {
        menuMapper.updateById(sysMenu);
        return true;
    }

    @Override
    public SysMenu getSysMenu(Long menuId) {
        return menuMapper.selectById(menuId);
    }

    @Override
    public Boolean addMenu(SysMenu sysMenu) {
        menuMapper.insert(sysMenu);
        return true;
    }

    @Override
    public boolean delByMenuIds(List<Long> menuIds) {
        //查询出是否有关联的子id
        menuMapper.deleteBatchIds(menuIds);
        return true;
    }

    /**
     * 构建树列表
     *
     * @param sysMenus
     * @return
     */
    private List<Tree<Long>> buildTree(List<SysMenu> sysMenus) {
        return TreeUtil.build(sysMenus, 0L,
                new TreeNodeConfig().setDeep(10),      //遍历深度
                (tree, treeNode) -> {
                    treeNode.setId(tree.getMenuId());
                    treeNode.setParentId(tree.getParentId());
                    treeNode.putExtra("orderNum", tree.getOrderNum());
                    treeNode.putExtra("menuName",tree.getMenuName());
                    treeNode.putExtra("path",tree.getPath());
                    treeNode.putExtra("component",tree.getComponent());
                    treeNode.putExtra("query",tree.getQuery());
                    treeNode.putExtra("menuType",tree.getMenuType());
                    treeNode.putExtra("isFrame",tree.getIsFrame());
                    treeNode.putExtra("icon",tree.getIcon());
                    treeNode.putExtra("status",tree.getStatus());

                }
        );
    }

    /**
     * 构建菜单路由树
     *
     * @param sysMenus
     * @return
     */
    private List<Tree<Long>> buildMenuTree(List<SysMenu> sysMenus) {
        return TreeUtil.build(sysMenus, 0L,
                new TreeNodeConfig().setDeep(10),      //遍历深度
                (tree, treeNode) -> {
                    treeNode.setId(tree.getMenuId());
                    treeNode.setParentId(tree.getParentId());
                    treeNode.putExtra("orderNum", tree.getOrderNum());
                    treeNode.putExtra("menuName",tree.getMenuName());
                    treeNode.putExtra("path",tree.getPath());
                    treeNode.putExtra("component",tree.getComponent());
                    treeNode.putExtra("query",tree.getQuery());
                    treeNode.putExtra("menuType",tree.getMenuType());
                    treeNode.putExtra("isFrame",tree.getIsFrame());
                    treeNode.putExtra("icon",tree.getIcon());
                }
        );
    }


}
